package matt.design;
import android.app.Activity;
import android.content.Context;
/** NEEDED FOR DATA BASE
 * 
 * import android.database.Cursor;
 **/
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * This class controls various new user functions and what happens
 * when various buttons on the page are clicked
 * 
 * Version 1.0 - Each button click returns the user to the login page
 * 
 * @author Crunch Time
 * @version 1.0
 */
public class RegisterActivity extends Activity {
	
	private UsersDataSource datasource;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        
        datasource = new UsersDataSource(this);
        datasource.open();
        
        Button registerButton = (Button) findViewById(R.id.btnRegister);
        Button loginScreen = (Button) findViewById(R.id.btn_return_to_login);
        final EditText first = (EditText) findViewById(R.id.reg_firstname);
        final EditText last = (EditText) findViewById(R.id.reg_lastname);
        final EditText mail = (EditText) findViewById(R.id.reg_email);
        final EditText pass1 = (EditText) findViewById(R.id.reg_pass1);
        final EditText pass2 = (EditText) findViewById(R.id.reg_pass2);;
 
        // Listening to Login button link
        
        registerButton.setOnClickListener(new View.OnClickListener() {
			
        	/**
			 * Returns the user to the login screen
			 * 
			 * @param v
			 */
			public void onClick(View v) {
                // Closing registration screen
				// Switching to Login Screen/closing register screen
				String firstname = first.getText().toString();
				String lastname = last.getText().toString();
				String email = mail.getText().toString();
				String password1 = pass1.getText().toString();
				String password2 = pass2.getText().toString();
//				User new_user = new User(firstname, lastname, email, password1);
//				UserManager users = UserManager.getUsers();
				/** USE ONLY WHEN DATABASE IS NEEDED
				 * 
				 * DBAdapter db = DBAdapter.getInstance();
				 **/
				
				Context context = getApplicationContext();
				
            	if (firstname.equals("") || lastname.equals("") || email.equals("") || password1.equals("") || password2.equals("")) {
            		Toast toast = Toast.makeText(context, "All fields required", Toast.LENGTH_SHORT);
            		toast.show();
            	}
            	else if (!password1.equals(password2)) {
            		Toast toast = Toast.makeText(context, "Passwords do not match", Toast.LENGTH_SHORT);
            		toast.show();
            	}
            	//else if (users.containsUser(email)) {
            	else if (datasource.containsUser(email)) {
            		Toast toast = Toast.makeText(context, "That email has already been registered", Toast.LENGTH_SHORT);
            		toast.show();
            	}
            	else {
    				/** USE THIS METHOD WHEN DATABSE IS TO BE USED
    				 * 
    				 * addUser(db, email, password1, firstname, lastname, email );
    				 * **/
            		//users.addUser(new_user);
            		
            		datasource.createUser(email, firstname, lastname, password1);
            		
            		finish();
            	}
			}
		});
        
        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {
 
        	/**
			 * Returns the user to the login screen
			 * 
			 * @param v
			 */
            public void onClick(View arg0) {
                // Closing registration screen
                // Switching to Login Screen/closing register screen

				finish();
            }
        });
    }
    
    
/**  WORKING DATABASE FUNCTIONS ~TO BE IMPLEMENTED LATER~
   
   public void displayUsers(Cursor c){
	    Toast.makeText(this,"id: " + c.getString(0) + "\n" + "Username: " + c.getString(1) + "\n" + "Password: " + c.getString(2) + "\n" +
	    "First/Last Name: " + c.getString(3) + " " + c.getString(4) + "\n" + "Email" + c.getString(5), Toast.LENGTH_SHORT).show();
    }
    
    public void addUser(DBAdapter db, String username, String password, String fName, String lName, String email){
    	db.open();
    	db.insertTitle(username, password, fName, lName, email);
    	//display all users every time you add a new user
    	Cursor c = db.getAllTitles();
    	if (c.moveToFirst()){
	        do {
	        	displayUsers(c);
	        } while (c.moveToNext());
        }
        db.close();
    }
**/
}
